package com.wswy.wzcx.ui.persenter;

import android.app.Activity;
import android.content.DialogInterface;
import android.util.Log;

import com.che.libcommon.utils.ToastUtils;
import com.che.libcommon.utils.optional.Optional;
import com.wswy.wzcx.R;
import com.wswy.wzcx.api.Api;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.module.ScTimes;
import com.wswy.wzcx.ui.other.DialogFactory;
import com.wswy.wzcx.ui.view.SettingView;

import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;

/**
 * Created by wangduheng26 on 2018/1/25.
 * wangduheng26@gmail.com
 */

public class SettingPresenter {
    private SettingView mview;

    public SettingPresenter(SettingView view) {
        this.mview = view;
    }

    public void showLogoutDialog() {
        DialogFactory.create(mview.activity(), R.string.logout, "确定退出登录吗？", R.string.ok, R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                actionLogout();
            }
        }).show();
    }

    public void actionLogout() {
        Api.get().submitLogout().subscribe(new SingleObserver<Optional<String>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onSuccess(Optional<String> optional) {
                //if (optional != null && optional.isPresent()) {
                    ToastUtils.showText("退出成功");
                ScTimes.addLogout();
                Activity activity = mview.activity();
                if (activity != null && !activity.isFinishing()){
                    activity.finish();
                }
                    DataCenter.get().cleanUser();
                //}
            }

            @Override
            public void onError(Throwable e) {
                Log.e("wzcx", "e:"+e);
            }
        });
    }
}
